package com.lyy.controller;

import cn.hutool.core.util.ObjectUtil;
import com.lyy.entity.Util.LocalFileUtil;
import com.lyy.result.Result;
import lombok.SneakyThrows;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.File;
import java.nio.channels.MulticastChannel;
import java.util.UUID;

/**
 * @author Liangyaoyun
 */
@RequestMapping("/api/v1/avatar")
public class AvatarController {
    //被删除的图片位置
    private static final String AVATAR_DIR ="C:\\Users\\10590\\Desktop\\文件合集\\报告与实验" ;

    @ResponseBody
    @SneakyThrows
    @RequestMapping("/upload")
    public Result upload(
            @RequestParam("avatar")MultipartFile multipartFile
            ){
            //获取文件名
        String  filename=multipartFile.getOriginalFilename();

        if(ObjectUtil.isEmpty(filename))
        {
            throw new RuntimeException("文件名不能为空");
        }
        String suffix=filename.substring(filename.lastIndexOf("."));
        String part1= UUID.randomUUID().toString().replace("-","").substring(20);
        String part2=System.currentTimeMillis()+"";
        multipartFile.transferTo(new File("C:\\Users\\10590\\Desktop\\文件合集\\PPT",part1+part2+suffix));

        return Result.success();
    }
    @ResponseBody
    @SneakyThrows
    @RequestMapping("/upload01")
    public Result upload01(
            @RequestParam("avatar")MultipartFile multipartFile
    ){
        //获取文件名
        String filename= LocalFileUtil.upload(multipartFile,"C:\\Users\\10590\\Desktop\\文件合集\\报告与实验");
        return Result.success();
    }
    @ResponseBody
    @SneakyThrows
    @RequestMapping("/delete")
    public Result delete(@RequestParam String filename){

        return LocalFileUtil.delete(filename,AVATAR_DIR)?Result.success(filename):Result.fail();
    }
    @ResponseBody
    @SneakyThrows
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(@RequestParam String filename){
        return LocalFileUtil.download(filename,AVATAR_DIR);
    }

}
